package com.yuxin.tongyi.lsll.sub;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.yuxin.tongyi.lsll.R;
import com.yuxin.tongyi.lsll.common.Constants;
import com.yuxin.tongyi.lsll.common.TongyiTeaApplication;
import com.yuxin.tongyi.lsll.pojo.LifeInfo;
import com.yuxin.tongyi.lsll.pojo.LoginInfo;
import com.yuxin.tongyi.lsll.util.PubUtil;

/**
 * @author ww
 * @date:2012-3-15 下午02:48:55
 * @Desc:
 * @version :
 * 
 */
public class DayLifeDetailActivity extends BaseActivity {
	
	private Button btnOk, btnBack;
	private TextView tvDate, tvName;
	private Spinner spAttendence, spFoodStatus, spFoodCook, spSleepStatus, spHomeWork, spEdcution, spOpenair, spGame, spHealth, spCourse, spDriking, spMotions;
	private String[] arrAttendence, arrFoodStatus, arrFoodCook, arrSleepStatus, arrHomeWork, arrEdcution, arrOpenair, arrGame, arrHealth, arrCourse, arrDriking, arrMotions;
	private ArrayAdapter<String> adapterAttendence, adapterFoodStatus, adapterFoodCook, adapterSleepStatus, adapterHomeWork, adapterEdcution, adapterOpenair, adapterGame, adapterHealth, adapterCourse, adapterDriking, adapterMotions;
	private TextView tvAttendence, tvFoodStatus, tvFoodCook, tvSleepStatus, tvHomeWork, tvEdcution, tvOpenair, tvGame, tvHealth, tvCourse, tvDriking, tvMotions;
	
	private LoginInfo loginInfo;
	//private Intent BackIntent;
	private LifeInfo lifeinfo;
	private String currentStuId;
	private String currentGradeId;
	
	private EditText etRemark;
	private TableRow trDrinking, trMontions, trHomeWork;

	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sub_daylifedetail);
		PubUtil.showProgressDialog(DayLifeDetailActivity.this, Constants.MSG_LOADING, Constants.MSG_WAIT);
		
		lifeinfo = new LifeInfo();
		initView();
		initData();
		
		PubUtil.closeProgressDialog();
	}
	
	public void initView() {
		tvDate = (TextView) findViewById(R.id.sub_daylifedetail_tvDate);
		tvName = (TextView) findViewById(R.id.sub_daylifedetail_babyname);
		
		btnOk = (Button) findViewById(R.id.sub_daylifedetail_btnOk);
		btnBack = (Button) findViewById(R.id.sub_daylifedetail_btnBack);
		btnOk.setOnClickListener(new onBtnClickListener());
		btnBack.setOnClickListener(new onBtnClickListener());

		spAttendence = (Spinner) findViewById(R.id.sub_daylifedetail_spattendence);
		adapterAttendence = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		adapterAttendence.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spAttendence.setOnItemSelectedListener(new onItemSelectListener());
		tvAttendence = (TextView) findViewById(R.id.sub_daylifedetail_tvattendence);
		tvAttendence.setOnClickListener(new onTextClickListener());
		
		spFoodStatus = (Spinner) findViewById(R.id.sub_daylifedetail_spfoodStatus);
		spFoodStatus.setOnItemSelectedListener(new onItemSelectListener());
		adapterFoodStatus = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		adapterFoodStatus.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tvFoodStatus = (TextView) findViewById(R.id.sub_daylifedetail_tvfoodStatus);
		tvFoodStatus.setOnClickListener(new onTextClickListener());
		
		spFoodCook = (Spinner) findViewById(R.id.sub_daylifedetail_spfoodCook);
		spFoodCook.setOnItemSelectedListener(new onItemSelectListener());
		adapterFoodCook = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		adapterFoodCook.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tvFoodCook = (TextView) findViewById(R.id.sub_daylifedetail_tvfoodCook);
		tvFoodCook.setOnClickListener(new onTextClickListener());
		
		spSleepStatus = (Spinner) findViewById(R.id.sub_daylifedetail_spsleepStatus);
		spSleepStatus.setOnItemSelectedListener(new onItemSelectListener());
		adapterSleepStatus = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		adapterSleepStatus.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tvSleepStatus = (TextView) findViewById(R.id.sub_daylifedetail_tvsleepStatus);
		tvSleepStatus.setOnClickListener(new onTextClickListener());
		
		spHomeWork = (Spinner) findViewById(R.id.sub_daylifedetail_sphomeWork);
		spHomeWork.setOnItemSelectedListener(new onItemSelectListener());
		adapterHomeWork = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		adapterHomeWork.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tvHomeWork = (TextView) findViewById(R.id.sub_daylifedetail_tvhomeWork);
		tvHomeWork.setOnClickListener(new onTextClickListener());
		
		spEdcution = (Spinner) findViewById(R.id.sub_daylifedetail_spedcution);
		spEdcution.setOnItemSelectedListener(new onItemSelectListener());
		adapterEdcution = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		adapterEdcution.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tvEdcution = (TextView) findViewById(R.id.sub_daylifedetail_tvedcution);
		tvEdcution.setOnClickListener(new onTextClickListener());
		
		spOpenair = (Spinner) findViewById(R.id.sub_daylifedetail_spopenair);
		spOpenair.setOnItemSelectedListener(new onItemSelectListener());
		adapterOpenair = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		adapterOpenair.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tvOpenair = (TextView) findViewById(R.id.sub_daylifedetail_tvopenair);
		tvOpenair.setOnClickListener(new onTextClickListener());
		
		spGame = (Spinner) findViewById(R.id.sub_daylifedetail_spgame);
		spGame.setOnItemSelectedListener(new onItemSelectListener());
		adapterGame = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		adapterGame.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tvGame = (TextView) findViewById(R.id.sub_daylifedetail_tvgame);
		tvGame.setOnClickListener(new onTextClickListener());
		
		spHealth = (Spinner) findViewById(R.id.sub_daylifedetail_sphealth);
		spHealth.setOnItemSelectedListener(new onItemSelectListener());
		adapterHealth = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		adapterHealth.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tvHealth = (TextView) findViewById(R.id.sub_daylifedetail_tvhealth);
		tvHealth.setOnClickListener(new onTextClickListener());
		
		spCourse = (Spinner) findViewById(R.id.sub_daylifedetail_spcourse);
		spCourse.setOnItemSelectedListener(new onItemSelectListener());
		adapterCourse = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		adapterCourse.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tvCourse = (TextView) findViewById(R.id.sub_daylifedetail_tvcourse);
		tvCourse.setOnClickListener(new onTextClickListener());
		
		etRemark = (EditText) findViewById(R.id.sub_daylifedetail_etremark);

		spDriking = (Spinner) findViewById(R.id.sub_daylifedetail_spdriking);
		spDriking.setOnItemSelectedListener(new onItemSelectListener());
		adapterDriking = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		adapterDriking.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tvDriking = (TextView) findViewById(R.id.sub_daylifedetail_tvdriking);
		tvDriking.setOnClickListener(new onTextClickListener());
		
		spMotions = (Spinner) findViewById(R.id.sub_daylifedetail_spmotions);
		spMotions.setOnItemSelectedListener(new onItemSelectListener());
		adapterMotions = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		adapterMotions.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		tvMotions = (TextView) findViewById(R.id.sub_daylifedetail_tvmotions);
		tvMotions.setOnClickListener(new onTextClickListener());
		
		trHomeWork = (TableRow) findViewById(R.id.sub_daylifedetail_trhomeWork);
		trDrinking = (TableRow) findViewById(R.id.sub_daylifedetail_trdriking);
		trMontions = (TableRow) findViewById(R.id.sub_daylifedetail_trmotions);
	}

	private class onTextClickListener implements OnClickListener{

		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.sub_daylifedetail_tvattendence:
				spAttendence.performClick();
				break;
			case R.id.sub_daylifedetail_tvfoodStatus:
				spFoodStatus.performClick();
				break;
			case R.id.sub_daylifedetail_tvfoodCook:
				spFoodCook.performClick();
				break;
			case R.id.sub_daylifedetail_tvsleepStatus:
				spSleepStatus.performClick();
				break;
			case R.id.sub_daylifedetail_tvhomeWork:
				spHomeWork.performClick();
				break;
			case R.id.sub_daylifedetail_tvedcution:
				spEdcution.performClick();
				break;
			case R.id.sub_daylifedetail_tvopenair:
				spOpenair.performClick();
				break;
			case R.id.sub_daylifedetail_tvgame:
				spGame.performClick();
				break;
			case R.id.sub_daylifedetail_tvhealth:
				spHealth.performClick();
				break;
			case R.id.sub_daylifedetail_tvcourse:
				spCourse.performClick();
				break;
			case R.id.sub_daylifedetail_tvdriking:
				spDriking.performClick();
				break;
			case R.id.sub_daylifedetail_tvmotions:
				spMotions.performClick();
				break;
			default:
				break;
			}
		}
	}
	
	private class onItemSelectListener implements OnItemSelectedListener{
		@Override
		public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
			switch(adapterView.getId()){
			case R.id.sub_daylifedetail_spattendence:
				tvAttendence.setText(adapterAttendence.getItem(position));
				break;
			case R.id.sub_daylifedetail_spfoodStatus:
				tvFoodStatus.setText(adapterFoodStatus.getItem(position));
				break;
			case R.id.sub_daylifedetail_spfoodCook:
				tvFoodCook.setText(adapterFoodCook.getItem(position));
				break;
			case R.id.sub_daylifedetail_spsleepStatus:
				tvSleepStatus.setText(adapterSleepStatus.getItem(position));
				break;
			case R.id.sub_daylifedetail_sphomeWork:
				tvHomeWork.setText(adapterHomeWork.getItem(position));
				break;
			case R.id.sub_daylifedetail_spedcution:
				tvEdcution.setText(adapterEdcution.getItem(position));
				break;
			case R.id.sub_daylifedetail_spopenair:
				tvOpenair.setText(adapterOpenair.getItem(position));
				break;
			case R.id.sub_daylifedetail_spgame:
				tvGame.setText(adapterGame.getItem(position));
				break;
			case R.id.sub_daylifedetail_sphealth:
				tvHealth.setText(adapterHealth.getItem(position));
				break;
			case R.id.sub_daylifedetail_spcourse:
				tvCourse.setText(adapterCourse.getItem(position));
				break;
			case R.id.sub_daylifedetail_spdriking:
				tvDriking.setText(adapterDriking.getItem(position));
				break;
			case R.id.sub_daylifedetail_spmotions:
				tvMotions.setText(adapterMotions.getItem(position));
				break;
			}
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			
		}
		
	} 

	public void initData() {
		try {
			loginInfo = (LoginInfo) TongyiTeaApplication.getMap().get(Constants.GLOBAL_LOGININFO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Bundle bundle = getIntent().getExtras();
		if(bundle != null){
			tvDate.setText(bundle.getString("date"));
			tvName.setText(bundle.getString("name"));
			currentStuId = bundle.getString("id");
			currentGradeId = bundle.getString("gradeid");
			lifeinfo = (LifeInfo) bundle.getSerializable("lifeinfo");
		}
		
		int iGradeID = Integer.parseInt(currentGradeId);
		if(iGradeID >= 4){
			//大班  幼小衔接
			tvHomeWork.setVisibility(View.VISIBLE);
			tvDriking.setVisibility(View.GONE);
			tvMotions.setVisibility(View.GONE);
			
			trHomeWork.setVisibility(View.VISIBLE);
			trDrinking.setVisibility(View.GONE);
			trMontions.setVisibility(View.GONE);
		}else if(iGradeID>0 && iGradeID<4){
			//托 小 中
			tvHomeWork.setVisibility(View.GONE);
			tvDriking.setVisibility(View.VISIBLE);
			tvMotions.setVisibility(View.VISIBLE);
			
			trHomeWork.setVisibility(View.GONE);
			trDrinking.setVisibility(View.VISIBLE);
			trMontions.setVisibility(View.VISIBLE);
		}
		
		getAdapterArray();
		setAdapterData();
		setItemValue(lifeinfo);
	}
	
	private void getAdapterArray(){
		arrAttendence = getResources().getStringArray(R.array.attendence);
		arrFoodStatus = getResources().getStringArray(R.array.foodStatus);
		arrFoodCook = getResources().getStringArray(R.array.foodCook);
		arrSleepStatus = getResources().getStringArray(R.array.sleepStatus);
		arrHomeWork = getResources().getStringArray(R.array.homeWork);
		arrEdcution = getResources().getStringArray(R.array.edcution);
		arrOpenair = getResources().getStringArray(R.array.openair);
		arrGame = getResources().getStringArray(R.array.game);
		arrHealth = getResources().getStringArray(R.array.health);
		arrCourse = getResources().getStringArray(R.array.course);
		//arrRemark = getResources().getStringArray(R.array.remark);
		arrDriking = getResources().getStringArray(R.array.driking);
		arrMotions = getResources().getStringArray(R.array.motions);
	}
	
	private void setAdapterData(){
		for(int i=0; i<arrAttendence.length; i++){
			adapterAttendence.add(arrAttendence[i]);
		}
		for(int i=0; i<arrFoodStatus.length; i++){
			adapterFoodStatus.add(arrFoodStatus[i]);
		}
		for(int i=0; i<arrFoodCook.length; i++){
			adapterFoodCook.add(arrFoodCook[i]);
		}
		for(int i=0; i<arrSleepStatus.length; i++){
			adapterSleepStatus.add(arrSleepStatus[i]);
		}
		for(int i=0; i<arrHomeWork.length; i++){
			adapterHomeWork.add(arrHomeWork[i]);
		}
		for(int i=0; i<arrEdcution.length; i++){
			adapterEdcution.add(arrEdcution[i]);
		}
		for(int i=0; i<arrOpenair.length; i++){
			adapterOpenair.add(arrOpenair[i]);
		}
		for(int i=0; i<arrGame.length; i++){
			adapterGame.add(arrGame[i]);
		}
		for(int i=0; i<arrHealth.length; i++){
			adapterHealth.add(arrHealth[i]);
		}
		for(int i=0; i<arrCourse.length; i++){
			adapterCourse.add(arrCourse[i]);
		}
//		for(int i=0; i<arrRemark.length; i++){
//			adapterRemark.add(arrRemark[i]);
//		}
		for(int i=0; i<arrDriking.length; i++){
			adapterDriking.add(arrDriking[i]);
		}
		for(int i=0; i<arrMotions.length; i++){
			adapterMotions.add(arrMotions[i]);
		}
		spAttendence.setAdapter(adapterAttendence);
		spFoodStatus.setAdapter(adapterFoodStatus);
		spFoodCook.setAdapter(adapterFoodCook);
		spSleepStatus.setAdapter(adapterSleepStatus);
		spHomeWork.setAdapter(adapterHomeWork);
		spEdcution.setAdapter(adapterEdcution);
		spOpenair.setAdapter(adapterOpenair);
		spGame.setAdapter(adapterGame);
		spHealth.setAdapter(adapterHealth);
		spCourse.setAdapter(adapterCourse);
		//spRemark.setAdapter(adapterRemark);
		spDriking.setAdapter(adapterDriking);
		spMotions.setAdapter(adapterMotions);
	}

	private void setItemDefaultValue(){
		spAttendence.setSelection(0, true);
		spFoodStatus.setSelection(0, true);
		spFoodCook.setSelection(0, true);
		spSleepStatus.setSelection(0, true);
		spHomeWork.setSelection(0, true);
		spEdcution.setSelection(0, true);
		spOpenair.setSelection(0, true);
		spGame.setSelection(0, true);
		spHealth.setSelection(0, true);
		spCourse.setSelection(0, true);
		//spRemark.setSelection(0, true);
		spDriking.setSelection(0, true);
		spMotions.setSelection(0, true);
		etRemark.setText("");
	}
	
	private void setItemValue(LifeInfo lifeinfo){
		spAttendence.setSelection(adapterAttendence.getPosition(lifeinfo.getAttendence()));
		spFoodStatus.setSelection(adapterFoodStatus.getPosition(lifeinfo.getFoodStatus()));
		spSleepStatus.setSelection(adapterSleepStatus.getPosition(lifeinfo.getSleepStatus()));
		spHomeWork.setSelection(adapterHomeWork.getPosition(lifeinfo.getHomeWork()));
		spEdcution.setSelection(adapterEdcution.getPosition(lifeinfo.getEdcution()));
		spOpenair.setSelection(adapterOpenair.getPosition(lifeinfo.getOpenair()));
		spGame.setSelection(adapterGame.getPosition(lifeinfo.getGame()));
		spHealth.setSelection(adapterHealth.getPosition(lifeinfo.getHealth()));
		//spRemark.setSelection(adapterRemark.getPosition(lifeinfo.getRemark()));
		spDriking.setSelection(adapterDriking.getPosition(lifeinfo.getDriking()));
		spMotions.setSelection(adapterMotions.getPosition(lifeinfo.getMotions()));
		spFoodCook.setSelection(adapterFoodCook.getPosition(lifeinfo.getFoodCook()));
		spCourse.setSelection(adapterCourse.getPosition(lifeinfo.getCourse()));
		etRemark.setText(lifeinfo.getRemark());
	}
	
	private class onBtnClickListener implements OnClickListener {
		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.sub_daylifedetail_btnOk:
				
				setLifeInfo();
				
				Intent BackIntent = new Intent();
				BackIntent.putExtra("lifeinfo", lifeinfo);
				setResult(Activity.RESULT_OK, BackIntent);
				finish();
				break;
			case R.id.sub_daylifedetail_btnBack:
				finish();
				break;
			}
		}
	}
	
	private void setLifeInfo(){
		lifeinfo.setStudentId(currentStuId);
		lifeinfo.setStudentName(tvName.getText().toString());
		lifeinfo.setAttendence(spAttendence.getSelectedItem().toString());
		lifeinfo.setFoodStatus(spFoodStatus.getSelectedItem().toString());
		lifeinfo.setFoodCook(spFoodCook.getSelectedItem().toString());
		lifeinfo.setSleepStatus(spSleepStatus.getSelectedItem().toString());
		lifeinfo.setHomeWork(spHomeWork.getSelectedItem().toString());
		lifeinfo.setEdcution(spEdcution.getSelectedItem().toString());
		lifeinfo.setOpenair(spOpenair.getSelectedItem().toString());
		lifeinfo.setGame(spGame.getSelectedItem().toString());
		lifeinfo.setHealth(spHealth.getSelectedItem().toString());
		lifeinfo.setCourse(spCourse.getSelectedItem().toString());
		//lifeinfo.setRemark(spRemark.getSelectedItem().toString());
		lifeinfo.setDriking(spDriking.getSelectedItem().toString());
		lifeinfo.setMotions(spMotions.getSelectedItem().toString());
		lifeinfo.setRemark(etRemark.getText().toString());
	}
}
